import Mock from "mockjs";
//处理路径传参
import { parameteUrl } from "@/utils/query-param"
import enterprise from "./enterprise.json"
import activityList from "./activity.json"
import knowledgeList from "./knowledge.json"

function ArrSet(Arr: any[], id: string): any[] {
    let obj: any = {}
    const arrays = Arr.reduce((setArr, item) => {
        obj[item[id]] ? '' : (obj[item[id]] = true && setArr.push(item))
        return setArr
    }, [])
    return arrays
}
/**
* @description: min ≤ r ≤ max  随机数
* @param {*} Min
* @param {*} Max
* @return {*}
*/
function RandomNumBoth(Min: any, Max: any) {
    var Range = Max - Min;
    var Rand = Math.random();
    var num = Min + Math.round(Rand * Range); //四舍五入
    return num;
}
//饼图
export default [
    {
        url: "/bigscreen/countUserNum",
        type: "get",
        response: () => {
            const a = Mock.mock({
                success: true,
                data: {
                    offlineNum: '@integer(50, 100)',
                    alarmNum: '@integer(20, 100)',
                    lockNum: '@integer(10, 50)',
                    totalNum: 368
                }
            })
            a.data.onlineNum = a.data.totalNum - a.data.offlineNum - a.data.lockNum - a.data.alarmNum
            return a
        },
    },
    {
        url: "/bigscreen/countDeviceNum",
        type: "get",
        response: () => {
            const a = Mock.mock({
                success: true,
                data: {
                    alarmNum: '@integer(100, 1000)',
                    offlineNum: '@integer(0, 50)',
                    totalNum: 698
                }
            })
            a.data.onlineNum = a.data.totalNum - a.data.offlineNum
            return a
        }
    },
    //左下
    {
        url: "/bigscreen/leftBottom",
        type: "get",
        response: () => {
            const a = Mock.mock({
                success: true,
                data: {
                    "list|20": [
                        {
                            provinceName: "@province()",
                            cityName: '@city()',
                            countyName: "@county()",
                            createTime: "@datetime('yyyy-MM-dd HH:mm:ss')",
                            deviceId: "6c512d754bbcd6d7cd86abce0e0cac58",
                            "gatewayno|+1": 10000,
                            "onlineState|1": [0, 1],

                        }
                    ]
                }
            })
            return a
        }
    },
    //折线图
    {
        url: "/bigscreen/alarmNum",
        type: "get",
        response: () => {
            const a = Mock.mock({
                success: true,
                data: {
                    dateList: ['2023-11', '2023-12', '2024-01', '2024-02', '2024-03', "2024-04"],
                    "numList|6": [
                        '@integer(0, 1000)'
                    ],
                    "numList2|6": [
                        '@integer(0, 1000)'
                    ]
                }
            })
            return a
        }
    },
    //条形图
    {
        url: "/bigscreen/ranking",
        type: "get",
        response: () => {
            let num = Mock.mock({ "list|80": [{ value: "@integer(50,1000)", name: "@city()" }] }).list
            //   console.log("ranking",num);
            let newNum: any = [], numObj: any = {}
            num.map((item: any) => {
                if (!numObj[item.name] && newNum.length < 8) {
                    numObj[item.name] = true
                    newNum.push(item)
                }
            })
            let arr = newNum.sort((a: any, b: any) => {
                return b.value - a.value
            })
            let a = {
                success: true,
                data: arr
            }
            return a
        }
    },
    //右下
    {
        url: "/bigscreen/rightBottom",
        type: "get",
        response: () => {
            const a = Mock.mock({
                success: true,
                data: {
                    "list|40": [{
                        alertdetail: "@csentence(5,10)",
                        "alertname|1": ["水浸告警", "各种报警"],
                        alertvalue: "@float(60, 200)",
                        createtime: "2024-04-19 08:38:33",
                        deviceid: null,
                        "gatewayno|+1": 10000,
                        phase: "A1",
                        sbInfo: "@csentence(10,18)",
                        "terminalno|+1": 100,
                        provinceName: "@province()",
                        cityName: '@city()',
                        countyName: "@county()",
                    }],

                }
            })
            return a
        }
    },
    //安装计划
    {
        url: "/bigscreen/installationPlan",
        type: "get",
        response: () => {

            let num = RandomNumBoth(26, 32);
            const a = Mock.mock({
                ["category|" + num]: ["@city()"],
                ["barData|" + num]: ["@integer(10, 100)"],
            })
            let lineData = [], rateData = [];
            for (let index = 0; index < num; index++) {
                let lineNum = Mock.mock('@integer(0, 100)') + a.barData[index]
                lineData.push(lineNum)
                let rate = a.barData[index] / lineNum;
                rateData.push((rate * 100).toFixed(0))
            }
            a.lineData = lineData
            a.rateData = rateData
            return {
                success: true,
                data: a
            }
        }
    },
    {
        url: "/bigscreen/centerMap",
        type: "get",
        response: (options: any) => {
            let params = parameteUrl(options.url)
            //不是中国的时候
            if (params.regionCode && !["china"].includes(params.regionCode)) {
                const a = Mock.mock({
                    success: true,
                    data: {
                        "dataList|100": [
                            {
                                name: "@city()",
                                value: '@integer(1, 1000)'
                            }
                        ],
                        regionCode: params.regionCode,//-代表中国
                    }
                })
                return a
            } else {
                const a = Mock.mock({
                    success: true,
                    data: {
                        "dataList|12": [
                            {
                                name: "@province()",
                                value: '@integer(1, 1100)'
                            }
                        ],
                        regionCode: 'china',
                    }
                })
                // 去重
                a.data.dataList = ArrSet(a.data.dataList, "name")
                return a
            }
        }
    },
    //企业概况
    {
        url: "/enterprise/profile",
        type: "get",
        response: () => {
            let totalNum = 0;
            const groupByModel = enterprise.reduce((acc, person) => {
                const businessModel = person.businessModel;
                if (!acc[businessModel]) {
                    acc[businessModel] = 1;
                } else {
                    acc[businessModel]++;
                }
                totalNum++;
                return acc;
            }, {});
            const resultArray = [];
            for (const model in groupByModel) {
                if (groupByModel.hasOwnProperty(model)) {
                    resultArray.push({name: model, value: groupByModel[model]});
                }
            }

            const a = Mock.mock({
                success: true,
                data: {
                    totalNum: totalNum,
                    // list: [
                    // {
                    //     name: "自营",
                    //     value: 123
                    // },{
                    //     name: "控股",
                    //     value: 234
                    // },{
                    //     name: "入股",
                    //     value: 345
                    // }
                    // ]
                    list: resultArray
                }
            })
            return a
        }
    },
    //企业列表
    {
        url: "/enterprise/list",
        type: "get",
        response: (options: any) => {
            let params = parameteUrl(options.url)
            console.log(params);
            let list = enterprise;
            if(params.businessModel){
                list = list.filter((item:any)=>item.businessModel === params.businessModel)
            }
            if(params.personsScaleMin){
                list = list.filter((item:any)=>item.numberOfInsuredPersons >= params.personsScaleMin)
            }
            if(params.personsScaleMax){
                list = list.filter((item:any)=>item.numberOfInsuredPersons <= params.personsScaleMax)
            }
            if(params.enterpriseType){
                list = list.filter((item:any)=>item.enterpriseType === params.enterpriseType)
            }
            const data = [...list];
            console.log(list);
            const a = Mock.mock({
                success: true,
                data: {
                    list: data
                }
            })
            return a
        }
    },
     //企业详情
     {
        url: "/enterprise/view",
        type: "get",
        response: (options: any) => {
            let params = parameteUrl(options.url)
            const list = [];
            if(params.id){
                list.push(...enterprise.filter((item:any)=>item.id == params.id))
            }
            console.log(list);
            const a = Mock.mock({
                success: true,
                data: list.length>0?list[0]:{}
            })
            return a
        }
    },
    //企业规模
    {
        url: "/enterprise/scale",
        type: "get",
        response: (options: any) => {
            let params = parameteUrl(options.url)

            //企业人员规模
            let personsOne = 0;
            let personsTwo = 0;
            let personsThree = 0;
            let personsFour = 0;
            for (let index = 0; index < enterprise.length; index++) {
                const personsNum = parseInt(enterprise[index].numberOfInsuredPersons+'');
                //personNum转为数字
                if(personsNum < 20){
                    personsOne += 1
                }else if(personsNum >= 20 && personsNum < 50){
                    personsTwo += 1
                }else if(personsNum > 50 && personsNum < 100){
                    personsThree += 1
                }else{
                    personsFour += 1
                }
            }
            const personsScale = [
                {
                    name: "0-20人",
                    value: personsOne
                },{
                    name: "20-50人",
                    value: personsTwo
                },{
                    name: "50-100人",
                    value: personsThree
                },{
                    name: "100人以上",
                    value: personsFour
                }];

            
            //企业产值规模
            let outputValueOne = 0;
            let outputValueTwo = 0;
            let outputValueThree = 0;
            let outputValueFour = 0;
            for (let index = 0; index < enterprise.length; index++) {
                const outputValue = parseInt(enterprise[index].outputValue+'');
                //personNum转为数字
                if(outputValue < 100){
                    outputValueOne += outputValue
                }else if(outputValue >= 100 && outputValue < 500){
                    outputValueTwo += outputValue
                }else if(outputValue > 500 && outputValue < 1000){
                    outputValueThree += outputValue
                }else{
                    outputValueFour += outputValue
                }
            }
            const outputValueScale = [
                {
                    name: "0-100万元",
                    value: outputValueOne
                },{
                    name: "100-500万元",
                    value: outputValueTwo
                },{
                    name: "500-1000万元",
                    value: outputValueThree
                },{
                    name: "1000万元以上",
                    value: outputValueFour
                }];




            const a = Mock.mock({
                success: true,
                data: {
                    personsScale: personsScale,
                    outputValueScale: outputValueScale
                }
            })
            return a
        }
    },
    //企业类型
    {
        url: "/enterprise/type",
        type: "get",
        response: () => {
            const groupByModel = enterprise.reduce((acc, person) => {
                const enterpriseType = person.enterpriseType;
                if (!acc[enterpriseType]) {
                    acc[enterpriseType] = 1;
                } else {
                    acc[enterpriseType]++;
                }
                return acc;
            }, {});
            const resultArray = [];
            for (const model in groupByModel) {
                if (groupByModel.hasOwnProperty(model)) {
                    resultArray.push({name: model, value: groupByModel[model]});
                }
            }

            const a = Mock.mock({
                success: true,
                data: {
                    list: resultArray
                }
            })
            return a
        }
    },
    //季度产值
    {
        url: "/enterprise/sales",
        type: "get",
        response: () => {
            let oneList = [0,0,0,0];
            let twoList = [0,0,0,0];
            let threeList = [0,0,0,0];
            let fourList = [0,0,0,0];
            let fiveList = [0,0,0,0];

            for (let index = 0; index < enterprise.length; index++) {
                const element = enterprise[index];
                const sales = element.sales;
                oneList[0] += sales.one.total[0];
                oneList[1] += sales.one.total[1];
                oneList[2] += sales.one.total[2];
                oneList[3] += sales.one.total[3];
                twoList[0] += sales.two.total[0];
                twoList[1] += sales.two.total[1];
                twoList[2] += sales.two.total[2];
                twoList[3] += sales.two.total[3];
                threeList[0] += sales.three.total[0];
                threeList[1] += sales.three.total[1];
                threeList[2] += sales.three.total[2];
                threeList[3] += sales.three.total[3];
                fourList[0] += sales.four.total[0];
                fourList[1] += sales.four.total[1];
                fourList[2] += sales.four.total[2];
                fourList[3] += sales.four.total[3];
                fiveList[0] += sales.five.total[0];
                fiveList[1] += sales.five.total[1];
                fiveList[2] += sales.five.total[2];
                fiveList[3] += sales.five.total[3];
            }

            const a = Mock.mock({
                success: true,
                data: {
                    dateList: ['第一季度', '第二季度', '第三季度', '第四季度'],
                    oneList: oneList,
                    twoList: twoList,
                    threeList: threeList,
                    fourList: fourList,
                    fiveList: fiveList
                }
            })
            return a
        }
    },
    //企业区县销量
    {
        url: "/enterprise/district",
        type: "get",
        response: () => {
            const groupByModel = enterprise.reduce((acc, person) => {
                const district = person.district;
                if (!acc[district]) {
                    acc[district] = person.sales.total;
                } else {
                    acc[district]+= person.sales.total;
                }
                return acc;
            }, {});
            const resultArray = [];
            for (const model in groupByModel) {
                if (groupByModel.hasOwnProperty(model)) {
                    resultArray.push({name: model, value: groupByModel[model]});
                }
            }

            const a = Mock.mock({
                success: true,
                data: {
                    list: resultArray
                }
            })
            return a
        }
    },
    //企业综合服务
    {
        url: "/enterprise/integratedService",
        type: "get",
        response: () => {
            let landTransfer = 0;
            let landTrusteeship = 0;
            let smartAgriculture = 0;
            let mutualAid = 0;
            let financing = 0;
            let microloan = 0;
        
            for (let index = 0; index < enterprise.length; index++) {
                landTransfer += enterprise[index].integratedService.landTransfer;
                landTrusteeship += enterprise[index].integratedService.landTrusteeship;
                smartAgriculture += enterprise[index].integratedService.smartAgriculture;
                mutualAid += enterprise[index].financialService.mutualAid;
                financing += enterprise[index].financialService.financing;
                microloan += enterprise[index].financialService.microloan;
            }
            const integratedService = [
                {
                    name: "土地转让",
                    value: landTransfer
                },{
                    name: "土地托管",
                    value: landTrusteeship
                },{
                    name: "智慧农业",
                    value: smartAgriculture
                }
            ];
            const financialService = [
                {
                    name: "资金互助额",
                    value: mutualAid
                },{
                    name: "融资担保额",
                    value: financing
                },{
                    name: "小额贷款额",
                    value: microloan
                }
            ];

            const a = Mock.mock({
                success: true,
                data: {
                    integratedService: integratedService,
                    financialService: financialService
                }
            })
            return a
        }
    },
    //所有企业销售额
    {
        url: "/enterprise/turnover",
        type: "get",
        response: () => {
            let categoryList = [];
            let totalList = [];
            let oneList = [];
            let twoList = [];
            let threeList = [];
            let fourList = [];
            let fiveList = [];

            for (let index = 0; index < enterprise.length; index++) {
                const element = enterprise[index];
                categoryList.push(element.enterpriseName);
                totalList.push(element.sales.total);
                oneList.push(element.sales.one.total.reduce((accumulator, currentValue) => accumulator + currentValue, 0));
                twoList.push(element.sales.two.total.reduce((accumulator, currentValue) => accumulator + currentValue, 0));
                threeList.push(element.sales.three.total.reduce((accumulator, currentValue) => accumulator + currentValue, 0));
                fourList.push(element.sales.four.total.reduce((accumulator, currentValue) => accumulator + currentValue, 0));
                fiveList.push(element.sales.five.total.reduce((accumulator, currentValue) => accumulator + currentValue, 0));
            }

            const a = Mock.mock({
                success: true,
                data: {
                    categoryList,
                    totalList,
                    oneList: oneList,
                    twoList: twoList,
                    threeList: threeList,
                    fourList: fourList,
                    fiveList: fiveList
                }
            })
            return a
        }
    },
    //活动列表
    {
        url: "/activity/list",
        type: "get",
        response: () => {
            const a = Mock.mock({
                success: true,
                data: {
                    list: activityList
                }
            })
            return a
        }
    },
     //活动详情
     {
        url: "/activity/view",
        type: "get",
        response: (options: any) => {
            let params = parameteUrl(options.url)
            const list = [];
            if(params.id){
                list.push(...activityList.filter((item:any)=>item.id == params.id))
            }
            console.log(list);
            const a = Mock.mock({
                success: true,
                data: list.length>0?list[0]:{}
            })
            return a
        }
    },
    //重大活动
    {
        url: "/activity/important",
        type: "get",
        response: (options: any) => {
            let params = parameteUrl(options.url)
            const list = [];
            if(params.id){
                list.push(...activityList.filter((item:any)=>item.id == params.id))
            }else{
                let randomNumber = Math.floor(Math.random() * activityList.length);
                list.push(activityList[randomNumber])
            }
            console.log(list);
            const a = Mock.mock({
                success: true,
                data: list.length>0?list[0]:{}
            })
            return a
        }
    },
    //知识类型
    {
        url: "/knowledge/type",
        type: "get",
        response: () => {
            const group = knowledgeList.reduce((acc, item) => {
                const type = item.type;
                if (!acc[type]) {
                    acc[type] = 1;
                } else {
                    acc[type]++;
                }
                return acc;
            }, {});
            const resultArray = [];
            for (const type in group) {
                if (group.hasOwnProperty(type)) {
                    resultArray.push({name: type, value: group[type]});
                }
            }

            const a = Mock.mock({
                success: true,
                data: {
                    list: resultArray
                }
            })
            return a
        }
    },
     //知识列表
     {
        url: "/knowledge/list",
        type: "get",
        response: (options: any) => {
            let params = parameteUrl(options.url)
            console.log(params);
            let list = knowledgeList;
            if(params.type){
                list = list.filter((item:any)=>item.type === params.type)
            }
            const data = [...list];
            console.log(list);
            const a = Mock.mock({
                success: true,
                data: {
                    list: data
                }
            })
            return a
        }
    },
];

